package wykop.analyze;

import wykop.page.WykopComment;

public class NumberOfSubstrings extends NumericCommentFeature {

	private final String substring;
	private final String name;

	public NumberOfSubstrings(final String substring, final String name) {
		super();
		this.substring = substring;
		this.name = name;
	}

	@Override
	public String name() {
		return "NumberOf" + name;
	}

	@Override
	public int value(final WykopComment comment) {
		final String text = comment.getText();
		int ctr = -1;
		int total = 0;
		while (true) {
			if (ctr == -1) {
				ctr = text.indexOf(substring);
			} else {
				ctr = text.indexOf(substring, ctr);
			}
			if (ctr == -1) {
				break;
			} else {
				total++;
				ctr += substring.length();
			}
		}
		return total;
	}

}
